// pages/user_center/user_info/driver/info_editer/info_editer.js
Page({
  data:{
    placeHoder:'',
    formError:false,
    errorMessage:'表单错误',
  },
  onLoad:function(options){
      // 页面初始化 options为页面跳转所带来的参数
      var title = options.title;
      var param = options.param;
      var value = options.value;
      
      this.setData({
          title: title,
          param: param,
          value: value,
          placeHoder: title
      });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  submit:function(e){
    var object = e.detail.value;
    var userInfo = wx.getStorageSync('userInfo');
    var param = this.data.param;

    var that = this;

    wx.request({
      url: 'https://www.lanshuicar.com/Server/DriverPerson/DriverInfo.ashx',
      data: {
        id: userInfo.Id,
        stype: 1,
        [param]: object.param
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log('log',res.data);
        var code = res.data.code;
        if(code == 0){
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          });

          userInfo[param] = object.param;
          wx.setStorageSync('userInfo', userInfo);

          setTimeout(function(){
            wx.navigateBack();
          },1000)
          
        }else{
          that.setData({
            formError:true,
            errorMessage:res.data.msg
          });

          setTimeout(function(){
              that.setData({
                  formError: false,
                  errorMessage:'表单错误'
              });
          }, 3000);
        }
        
      },
      fail: function() {
        // fail
        that.setData({
          formError:true,
          errorMessage:'表单错误'
        });

        setTimeout(function(){
            that.setData({
                formError: false,
                errorMessage:'表单错误'
            });
        }, 3000);
      },
      complete: function() {
        // complete
      }
    })
  }
})